﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Threading;

namespace mediaPrime.Controls
{
    public class MPSideMenu : ListBox
    {
        public static readonly RoutedCommand MPMenuItemClickCommand = new RoutedCommand();

        public event mediaPrime.Controls.MPMenu.MPMenuItemClickedEventHandler MenuItemClicked;

        static MPSideMenu()
        {
            // Use another default style than the base class
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(
                typeof(MPSideMenu), new FrameworkPropertyMetadata(typeof(MPSideMenu))
            );
        }


        public MPSideMenu()
        {
            var mpMenuItemClickCommandBinding = new CommandBinding(MPMenuItemClickCommand);
            mpMenuItemClickCommandBinding.CanExecute += mpMenuItemClickCommandBinding_CanExecute;
            mpMenuItemClickCommandBinding.Executed += mpMenuItemClickCommandBinding_Executed;

            CommandBindings.Add(mpMenuItemClickCommandBinding);

            this.Loaded += MPSideMenu_Loaded;
        
        }


        private void MPSideMenu_Loaded(object sender, RoutedEventArgs e)
        {
            var traversalRequest = new TraversalRequest(FocusNavigationDirection.Next);
            MoveFocus(traversalRequest);
        }

        private void mpMenuItemClickCommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            if (e.OriginalSource is Button)
            {
                var dataContextButton = (e.OriginalSource as Button).DataContext;
                var listBoxItem = ItemContainerGenerator.ContainerFromItem(dataContextButton) as ListBoxItem;
                listBoxItem.IsSelected = true;
            }

            var menuItemClicked = MenuItemClicked;
            if (menuItemClicked != null)
                menuItemClicked(sender, new MPMenuItemClickedEventArgs() { Item = e.Parameter });
        }

        private void mpMenuItemClickCommandBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            if (e.OriginalSource is Button)
                e.CanExecute = (e.OriginalSource as Button).IsEnabled;
            else
                e.CanExecute = true;
        }

        protected override void OnItemsSourceChanged(System.Collections.IEnumerable oldValue, System.Collections.IEnumerable newValue)
        {
            base.OnItemsSourceChanged(oldValue, newValue);
            this.Dispatcher.BeginInvoke((Action)delegate
            {
                var traversalRequest = new TraversalRequest(FocusNavigationDirection.Previous);
                MoveFocus(traversalRequest);
            }, DispatcherPriority.Render);
        }


        

        protected override void OnPreviewKeyDown(KeyEventArgs e)
        {
            if (e.Key == Key.Right)
            {
                e.Handled = true;
                var traversalRequest = new TraversalRequest(FocusNavigationDirection.Right);
                MoveFocus(traversalRequest);
            }
            else if (e.Key == Key.Left)
            {
                e.Handled = true;
                var traversalRequest = new TraversalRequest(FocusNavigationDirection.Left);
                MoveFocus(traversalRequest);
            }

            base.OnPreviewKeyDown(e);
        }

    }
}
